package com.lhkj.ct.meta.modules.psychology.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lhkj.ct.base.mybatis.extension.service.impl.BatchHandleServiceImpl;
import com.lhkj.ct.meta.modules.gauge.common.enums.GaugeType;
import com.lhkj.ct.meta.modules.psychology.mapper.GaugeAnswerMapper;
import com.lhkj.ct.meta.modules.psychology.model.bo.AnswerForm;
import com.lhkj.ct.meta.modules.psychology.model.entity.TblGaugeAnswer;
import com.lhkj.ct.meta.modules.psychology.service.GaugeAnswerService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jobob
 * @since 2023-10-16
 */
@Service
public class GaugeAnswerServiceImpl extends BatchHandleServiceImpl<GaugeAnswerMapper, TblGaugeAnswer> implements GaugeAnswerService {


//    @Override
//    @Transactional
//    public void saveProcess(String reportId, GaugeType type, List<TblGaugeAnswer> answerRecord) {
//        Set<String> questions =answerRecord.stream().peek(p-> {
//            p.setRId(reportId);
//            p.setType(type);
//        }).map(TblGaugeAnswer::getQutId).collect(Collectors.toSet());
//        getBaseMapper().delete(new LambdaQueryWrapper<TblGaugeAnswer>()
//                .eq(TblGaugeAnswer::getRId, reportId)
//                .eq(TblGaugeAnswer::getType, type)
//                .in(TblGaugeAnswer::getQutId, questions)
//        );
//        saveBatch(answerRecord);
//    }

    @Override
    public void submit(String reportId, GaugeType type, List<TblGaugeAnswer> answerRecord) {
        getBaseMapper().delete(new LambdaQueryWrapper<TblGaugeAnswer>()
                .eq(TblGaugeAnswer::getRId, reportId)
                .eq(TblGaugeAnswer::getType, type)
        );
        List<TblGaugeAnswer> answers = answerRecord.stream().peek(p -> {
            p.setRId(reportId);
            p.setType(type);
        }).collect(Collectors.toList());
        saveBatch(answers);
    }

    @Override
    public Map<String, Object> convertAnswers2FormData(String rId, String gaugeId) {
        return getBaseMapper().convertAnswers2FormData(rId, gaugeId, GaugeType.SCALE)
                .stream()
                .collect(Collectors.toMap(AnswerForm::getName, AnswerForm::getValue, (a, b) -> a));
    }
}
